package com.youshang520i;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * Hello world!
 *
 * @author youshang
 */
@Configuration
@ComponentScan("com.youshang520i")
@EnableTransactionManagement
@MapperScan("com.youshang520i.dao")
public class AppTx {

    @Bean
//(destroyMethod = "close")
    DataSource dataSource() {
        BasicDataSource pool = new BasicDataSource();
        pool.setUsername("root");
        pool.setPassword("123456");
        pool.setDriverClassName("com.mysql.cj.jdbc.Driver");
        pool.setUrl("jdbc:mysql://192.168.64.4:3306/db_youshang?useUnicode=true&characterEncoding=utf-8");
        return pool;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {//DataSource dataSource
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public TransactionManager transactionManager(DataSource dataSource) {//DataSource dataSource
        return new DataSourceTransactionManager(dataSource);
    }


}
